package com.xuanyuan.util;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class SendMsg_webchinese {
	public static Map<String, Object> sendMessage(String userName,String password,String[] phoneNumbers,String messageContent)throws Exception{
		System.out.println("11111111");
		HttpClient client = new HttpClient();
		Map<String, Object> map = new HashMap<String, Object>();
		PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");
		
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=gbk");
		for (int i = 0; i < phoneNumbers.length; i++) {
			NameValuePair[] data = { 
					new NameValuePair("Uid",userName),
					new NameValuePair("Key", password),
					new NameValuePair("smsMob", phoneNumbers[i]),
					new NameValuePair("smsText",messageContent)};
			post.setRequestBody(data);
			client.executeMethod(post); 
		}
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		System.out.println("statusCode:" + statusCode);
		for (Header h : headers) {
			System.out.println(h.toString());
		}
		String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
		System.out.println("result:"+result);
		post.releaseConnection();
		if(Integer.parseInt(result)>0){
			map.put("flag", true);
		} else {
			map.put("flag", false);
		}
		map.put("result", result);
		return map;
	}
}
